package com.live.dengdengtv;

import android.app.Application;
import android.app.Notification;
import android.content.Context;
import android.widget.Toast;

import com.live.dengdengtv.activity.support.InitBusinessHelper;
import com.live.dengdengtv.kernel.utils.SxbLogImpl;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.umeng.socialize.PlatformConfig;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.https.HttpsUtils;

import java.util.concurrent.TimeUnit;

import cn.jpush.android.api.BasicPushNotificationBuilder;
import cn.jpush.android.api.CustomPushNotificationBuilder;
import cn.jpush.android.api.JPushInterface;
import okhttp3.OkHttpClient;


/**
 * 全局Application
 */
public class DengTvApplication extends Application {

    private static DengTvApplication app;
    private static Context context;


    public static Context getContext() {
        return context;
    }

    public static DengTvApplication getInstance() {
        return app;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        context = getApplicationContext();

////        LeakCanary.install(this);
        SxbLogImpl.init(getApplicationContext());
        InitBusinessHelper.initApp(getApplicationContext());
        initImageLoader();
        initOkHttpUtils();
        initUMengSocia();

//        JPushInterface.setDebugMode(false);
//        JPushInterface.init(this);
//        setStyleCustom();
    }

    /**
     *设置通知栏样式 - 定义通知栏Layout
     */
    private void setStyleCustom(){
        CustomPushNotificationBuilder builder =
                new CustomPushNotificationBuilder(
                        this,
                        R.layout.customer_notitfication_layout,
                        R.id.icon, R.id.title, R.id.text);
        builder.layoutIconDrawable = R.mipmap.ic_logo;
        builder.statusBarDrawable  = R.mipmap.ic_logo;
//        builder.developerArg0 = "developerArg2";
        JPushInterface.setDefaultPushNotificationBuilder( builder);
    }

    /**
     *设置通知提示方式 - 基础属性
     */
    private void setStyleBasic(){
        BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
        builder.statusBarDrawable = R.mipmap.ic_logo;
        builder.notificationFlags = Notification.FLAG_AUTO_CANCEL;  //设置为点击后自动消失
        builder.notificationDefaults = Notification.DEFAULT_SOUND;  //设置为铃声（ Notification.DEFAULT_SOUND）或者震动（ Notification.DEFAULT_VIBRATE）
        JPushInterface.setDefaultPushNotificationBuilder( builder);
    }

    private void initUMengSocia() {

//        微博：
//        App Key：183692353
//        App Secret：e38d8167bd1603a810f221819a229cbd


        //微信 appid appsecret    //TODO:微信：审核中
        PlatformConfig.setWeixin("wx967daebe835fbeac", "5bb696d9ccd75a38c8a0bfe0675559b3" );
        //新浪微博 appkey appsecret
        PlatformConfig.setSinaWeibo("183692353","e38d8167bd1603a810f221819a229cbd");
        // QQ和Qzone appid appkey
        PlatformConfig.setQQZone("1105554362", "LsyYwuT4Cr0b7doL");

    }

    private void initOkHttpUtils() {

        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)//设置可以访问所有HTTPS
                .connectTimeout(15000L, TimeUnit.MILLISECONDS)
                .readTimeout(15000L, TimeUnit.MILLISECONDS)
//                .cache(null)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);
    }

    private void initImageLoader() {
        // 初始化ImageLoader
        @SuppressWarnings("deprecation")
        DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.ic_stub) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.ic_empty) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.ic_error) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                // .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片
                .build(); // 创建配置过得DisplayImageOption对象

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).defaultDisplayImageOptions(options)
                .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);
    }

}
